package com.a3dweijing.wetouch2.Activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.a3dweijing.wetouch2.Common.CallBack;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.ProgressDialogHelp;
import com.a3dweijing.wetouch2.Util.Util;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.CardListMsg;
import com.a3dweijing.wetouch2.bean.ResultMsg;
import com.a3dweijing.wetouch2.datasave.Singleton;
import com.a3dweijing.wetouch2.network.Commsubscribe;
import com.a3dweijing.wetouch2.network.RetrofitUtil;

import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;

public class AddCardActivity extends BaseActivity
{

    public static final int ADDCARDACTIVITYCODE = 12;
    @BindView(R.id.back)
    ImageView back;
    
    @BindView(R.id.top_text)
    TextView topText;
    
    @BindView(R.id.top_right)
    TextView topRight;
    
    @BindView(R.id.et_yanzhengma)
    EditText etYanzhengma;
    
    @BindView(R.id.bt_yanzhengma)
    TextView btYanzhengma;
    
    @BindView(R.id.submit)
    Button submit;
    
    private int[] ids;
    
    private ProgressDialog dialog;
    
    private MyHandler handler;
    
    private int time = 60;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_card);
        ButterKnife.bind(this);
        initView();
    }
    
    private void isDateOk()
    {
        for (int i = 0; i < ids.length; i++)
        {
            EditText view = (EditText)getView(ids[i]);
            String s = view.getText().toString();
            if (TextUtils.isEmpty(s))
            {
                Toast.makeText(this, "您的数据未填写完整", Toast.LENGTH_SHORT).show();
                return;
            }
            String s1 = etYanzhengma.getText().toString();
            if (TextUtils.isEmpty(s1))
            {
                Toast.makeText(this, "请填写验证码", Toast.LENGTH_SHORT).show();
                return;
            }
            submit();
        }
        
    }

    public static void startActivity(Activity activity) {
        Intent intent=new Intent(activity,AddCardActivity.class);
        activity.startActivityForResult(intent,ADDCARDACTIVITYCODE);
    }
    
    private void submit()
    {
        Observable<ResultMsg> cardListMsgObservable = RetrofitUtil.getInstance().getApiOfPhp().bankCard(
            ((EditText)getView(R.id.phone_number)).getText().toString(),
            ((EditText)getView(R.id.car_number)).getText().toString(),
            ((EditText)getView(R.id.user_number)).getText().toString(),
            ((EditText)getView(R.id.name)).getText().toString(),
            Singleton.getInstance().getUser().getUser_id(),
            etYanzhengma.getText().toString());
        new Commsubscribe<ResultMsg>()
            .createSubscribe(cardListMsgObservable, dialog, null, disposables, this, new CallBack()
            {
                @Override
                public void doNext(int what, Object obj)
                {
                    ResultMsg r = (ResultMsg)obj;
                    Toast.makeText(AddCardActivity.this, r.getMessage(), Toast.LENGTH_SHORT).show();
                    if (r.isFlag()){
                        setResult(RESULT_OK);
                        finish();}
                }
            });
    }
    
    private void initView()
    {
        
        handler = new MyHandler(AddCardActivity.this);
        dialog = ProgressDialogHelp.createDefault(this);
        topText.setText("绑定银行卡");
        ids = new int[] {R.id.name, R.id.car_number, R.id.user_number, R.id.phone_number};
        String[] names = new String[] {"持卡人:", "卡号:", "身份证号码:", "手机号:"};
        for (int i = 0; i < ids.length; i++)
        {
            View viewById = findViewById(ids[i]);
            TextView textView = viewById.findViewById(R.id.tv_left);
            EditText editText = viewById.findViewById(R.id.et_rit);
            textView.setText(names[i]);
            switch (i)
            {
                case 1:
                    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                    break;
                case 3:
                    editText.setInputType(InputType.TYPE_CLASS_PHONE);
                    break;
            }
        }
    }
    
    private View getView(int id)
    {
        View viewById = findViewById(id);
        return viewById.findViewById(R.id.et_rit);
    }
    
    @OnClick({R.id.back, R.id.bt_yanzhengma, R.id.submit})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.submit:
                isDateOk();
                break;
            case R.id.back:
                finish();
                break;
            case R.id.bt_yanzhengma:
                EditText editText = (EditText)getView(R.id.phone_number);
                String s = editText.getText().toString();
                if (Util.isPhoneNumber(s))
                    getYanZhenMa(s);
                else
                    Toast.makeText(this, "请输入正确的手机号码...", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    
    /**
     * 获取验证码
     */
    public void getYanZhenMa(String s)
    {
        Observable<ResultMsg> observable = RetrofitUtil.getInstance().getApiOfPhp().getverificationCode(s);
        new Commsubscribe<ResultMsg>().createSubscribe(observable, dialog, null, disposables, this, new CallBack()
        {
            @Override
            public void doNext(int what, Object obj)
            {
                if (obj != null)
                {
                    ResultMsg msg = (ResultMsg)obj;
                    Toast.makeText(AddCardActivity.this, msg.getMessage(), Toast.LENGTH_SHORT).show();
                    if (msg.isFlag())
                    {
                        setBtYanzhengma(false);
                        handler.postDelayed(runnable, 1000);
                    }
                }
            }
        });
    }
    
    Runnable runnable = new Runnable()
    {
        @Override
        public void run()
        {
            time--;
            if (time > 0)
            {
                btYanzhengma.setText(time + "秒后重试");
                handler.postDelayed(this, 1000);
            }
            else
            {
                btYanzhengma.setText("获取验证码");
                time = 60;
                setBtYanzhengma(true);
            }
            
        }
    };
    
    private void setBtYanzhengma(boolean b)
    {
        btYanzhengma.setEnabled(b);
        if (b)
        {
            btYanzhengma.setBackgroundDrawable(getResources().getDrawable(R.drawable.baise_yuanjiao));
            btYanzhengma.setTextColor(getResources().getColor(R.color.background_b2));
            btYanzhengma.setText("获取验证码");
        }
        else
        {
            btYanzhengma.setBackgroundDrawable(getResources().getDrawable(R.drawable.huise_yuanjiao));
            btYanzhengma.setTextColor(getResources().getColor(R.color.white));
            btYanzhengma.setText(time + "");
        }
    }
    
    private static class MyHandler extends Handler
    {
        private final WeakReference<AddCardActivity> mActivity;
        
        private MyHandler(AddCardActivity mActivity)
        {
            this.mActivity = new WeakReference<>(mActivity);
        }
        
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case 1:
            }
            super.handleMessage(msg);
        }
    }
    
    @Override
    protected void onDestroy()
    {
        handler.removeCallbacks(runnable);
        super.onDestroy();
    }
}
